/*
	mLife "Game of Life" simulation written on the mFoundry MOJAX platform
	by: Christian Walker
	http://www.walkerarts.com
	http://www.mfoundry.com

   Rules:
   1. Any live cell with fewer than two neighbours dies, as if by loneliness.
   2. Any live cell with more than three neighbours dies, as if by overcrowding.
   3. Any live cell with two or three neighbours lives, unchanged, to the next generation.
   4. Any dead cell with exactly three neighbours comes to life.
*/
int row = 0;
int col = 0;
int rows = 16;
int cols = 16;

function down() {
	row++;
	if (row == rows) row = 0;
	main.focus = getCurrent();
}
function up() {
	row--;
	if (row < 0) row = rows-1;
	main.focus = getCurrent();
}
function left() {
	col--;
	if (col < 0) col = cols - 1;
	main.focus = getCurrent();
}
function right() {
	col++;
	if (col == cols) col = 0;
	main.focus = getCurrent();
}
function getCurrent() {
	return main.getElementById('thegrid').children[row].children[col];
}

grid.prototype = new Box();
function grid() {
	this.layout = "vertical";
	this.width="100%";
	this.height ="100%";
	this.valign = "center";
	
	for (var r=0; r<rows; r++) {
		Box row = new Box();
		row.class = "row";
		row.layout = "horizontal";
		row.width = "100%";
		row.height = "100%";
		row.halign="center";
		
		this.push(row);
		for (var c=0; c<cols; c++) {
			Box o = new organism();
			o.class = "cell";
			o.focusable = "true";
			o.width = "100%";
			o.height = "100%";
			
			row.push(o);
		}
	}
	
	initArrays();
}

// the heartbeat of life (run from a Timer)
function tick() {
	for (var r=0; r<rows; r++) {
		int uprow = r-1;
		int downrow = r+1;
		// loop around (asteroids style)
		if (uprow < 0) uprow = rows-1;
		if (downrow == rows) downrow = rows-1;
		
		for (var c=0; c<cols; c++) { 
			int leftcell = c-1;
			int rightcell = c+1;
			// loop around (asteroids style)
			if (leftcell < 0) leftcell = 0;
			if (rightcell == cols) rightcell = cols-1;
			
			// calc num friends
			int numfriends = 0;
			if (currentlife[uprow*cols+leftcell] == 1) numfriends++;
			if (currentlife[uprow*cols+c] == 1) numfriends++;
			if (currentlife[uprow*cols+rightcell] == 1) numfriends++;
			
			if (currentlife[r*cols+leftcell] == 1) numfriends++;
			if (currentlife[r*cols+rightcell] == 1) numfriends++;
			
			
			if (currentlife[downrow*cols+leftcell] == 1) numfriends++;
			if (currentlife[downrow*cols+c] == 1) numfriends++;
			if (currentlife[downrow*cols+rightcell] == 1) numfriends++;
			
			// IT'S LIFE OR DEATH
			// check for death by boredom
			if (numfriends < 2) secondlife[r*cols+c] = 0;
			else
			// check for death by overpartying
			if (numfriends > 3) secondlife[r*cols+c] = 0;
//			else
			// check for unchanged
//			if (currentlife[r*cols+c] == 1) && (numfriends == 2 || numfriends == 3) {//unchanged}
			else
			// check for bowchickawowwow
			if (numfriends == 3) secondlife[r*cols+c] = 1;
			
		}
	}
}

// the life arrays
currentlife = new Array(rows*cols);
secondlife = new Array(rows*cols);
function initArrays() {
	for (var r=0; r<rows; r++) {
		for (var c=0; c<cols; c++) {
			currentlife[r*cols+c] = 0;
			secondlife[r*cols+c] = 0;
		}
	}
}

/*
 * copies secondlife to the grid, and to the currentlife
 */
function update() {
	for (var r=0; r<rows; r++) {
		int rpos = r*cols;
		for (var c=0; c<cols; c++) {
			if (secondlife[rpos+c] == 1) main.getElementById('thegrid').children[r].children[c].class = "livecell";
			else
			main.getElementById('thegrid').children[r].children[c].class = "cell";
			
			currentlife[rpos+c] = secondlife[rpos+c];
		}
	}
}

function clear() {
	for (var r=0; r<rows; r++) {
		for (var c=0; c<cols; c++) {
			main.getElementById('thegrid').children[r].children[c].class = "cell";
		}
	}
}

// organism constructor
organism.prototype = new Box();
function organism() {
	this.name = "organism";
}
organism.prototype.onClick = function() {
	this.class = "livecell";
	pos = row*cols+col;
	currentlife[pos] = 1;
};